﻿using Microsoft.CodeAnalysis;

namespace Beeflys.Generator
{
    static class TypeSymbolExtensions
    {
        public static string GetFullNameExt(this ITypeSymbol typeSymbol)
        {
            string type = SystemDefaultTypeHelper.GetSystemType(typeSymbol.ToString());
            if (string.IsNullOrEmpty(type))
            {
                type = typeSymbol.ToString();
            }
            
            return type;
        }

        public static UTypeInfo ToTypeInfo(this ITypeSymbol typeSymbol_)
        {
            UTypeInfo temp = null;
            if (typeSymbol_ is INamedTypeSymbol namedTypeSymbol)
            {
                var fullName = namedTypeSymbol.GetFullNameExt();
                if (Mapper.Contains(fullName))
                {
                    temp = Mapper.Get(fullName);
                }
                else
                {
                    temp = new UTypeInfo(namedTypeSymbol);
                    temp.Parse();
                    
                    Mapper.Add(temp);
                }
            }
            else
            {
                temp = new UTypeInfo(typeSymbol_);
                temp.Parse();
            }

            return temp;
        }

        public static (string _namespace, string name) ResolveNameExt(this INamedTypeSymbol typeSymbol)
        {
            string fullName = typeSymbol.ToString();
            string name = typeSymbol.Name;
            return ResolveName(fullName, name);
        }

        private static (string _namespace, string name) ResolveName(string fullName, string name)
        {
            string _namespace = string.Empty;
            if (fullName != name)
                _namespace = fullName.Replace($".{name}", "");

            return (_namespace, name);
        }
    }
}